package com.astrob.activitys;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.astrob.NavFrameSDK;
import com.astrob.model.LonLat;
import com.astrob.model.Msg;
import com.astrob.naviframe.Start;
import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.astrob.util.Utils;
import com.astrob.view.Compass;
import com.astrob.view.MapViewS;
import com.astrob.view.MapViewS.MapCallBack;
import com.astrob.view.TipInfoView;
import com.astrob.view.ZoomView;
import com.nhc.activity_fragment.MarkerPointActivity;
import com.nhc.database.dbmodel.Route;
import com.nhc.database.dbmodel.WayPoint;
import com.nhc.database.manager.DBDataCenter;
import com.nhc.model.MapSetInfo;
import com.nhc.view.MultiMapView;

import org.mapsforge.core.model.LatLong;
import org.mapsforge.core.util.MercatorProjection;
import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
import org.mapsforge.map.layer.Layer;
import org.osmdroid.api.IGeoPoint;
import org.osmdroid.events.MapListener;
import org.osmdroid.events.ScrollEvent;
import org.osmdroid.events.ZoomEvent;
import org.osmdroid.util.BoundingBox;
import org.osmdroid.views.MapView;
import org.osmdroid.views.Projection;
import org.osmdroid.views.overlay.Overlay;

import java.util.ArrayList;

import zuo.biao.library.ui.AlertDialog;

public class DMapActivity extends Activity implements MapCallBack, View.OnClickListener, AlertDialog.OnDialogButtonClickListener {

    public static final String INTENT_ONMAP_NAME = "INTENT_ONMAP_NAME";
    public static final String INTENT_ONMAP_LONLAT = "INTENT_ONMAP_LONLAT";
    public static final String INTENT_ONMAP_FOROMROUTE = "INTENT_ONMAP_FOROMROUTE";//航线onmap
    public static final String INTENT_ONMAP_ONLYSELECT = "INTENT_ONMAP_ONLYSELECT";//点击tip后，直接返回

    public static final String INTENT_ONMAP_FROMEDIT = "INTENT_ONMAP_FROMEDIT";

    public static final String INTENT_ONMAP_FROMADD = "INTENT_ONMAP_FROMADD";

    public static Intent createIntent(Context context, String name, LonLat ll) {
        return new Intent(context, DMapActivity.class)
                .putExtra(INTENT_ONMAP_NAME, name)
                .putExtra(INTENT_ONMAP_LONLAT, ll);
    }

    public static Intent createIntentFromEdit(Context context) {
        return new Intent(context, DMapActivity.class)
                .putExtra(INTENT_ONMAP_FROMEDIT, true);
    }

    public static Intent createIntentFromRoute(Context context, boolean fromRoute) {
        return new Intent(context, DMapActivity.class)
                .putExtra(INTENT_ONMAP_FOROMROUTE, fromRoute);
    }

    //地图选点，导出DBDataCenter.getInstance().makerSeach
    public static Intent createIntentFromAdd(Context context) {
        return new Intent(context, DMapActivity.class)
                .putExtra(INTENT_ONMAP_FROMADD, true);
    }

    boolean _fromRoute = false;//航线onmap
    boolean _fromEdit = false;
    boolean _fromAdd = false;

    boolean _onlySelect = false;

    MultiMapView mMultiMapView;
    ZoomView mZoom;
    TipInfoView mTipInfo;
    TextView mScale;
    TextView tv_center_lonlat;
    TextView tv_centertolocal_distance;
    Button iv_move;
    Button iv_addpttoline;

    Button iv_addpt;
    TextView mLocal;

    NavFrameSDK.POIDescription _OriginalPt;//原始GPS坐标

    TextView iv_navi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dmap_activity);
        mMultiMapView = (MultiMapView) findViewById(R.id.id_multimap);
        mMultiMapView.setMapsource(SysUtils.getInstance().mapsource);
        initMapEngin();

        mTipInfo = (TipInfoView) findViewById(R.id.id_tipinfoview);
        mZoom = (ZoomView) findViewById(R.id.id_zoomview);
        mScale = (TextView) findViewById(R.id.id_map_scalebar);
        tv_center_lonlat = (TextView) findViewById(R.id.tv_center_lonlat);
        tv_centertolocal_distance = (TextView) findViewById(R.id.tv_centertolocal_distance);
        mLocal = (TextView) findViewById(R.id.btnlocal);

        iv_addpt = (Button) findViewById(R.id.iv_addpt);
        iv_navi = (Button) findViewById(R.id.iv_navi);
        iv_move = (Button) findViewById(R.id.iv_move);
        iv_addpttoline = (Button) findViewById(R.id.iv_addpttoline);
//        iv_navi.setTypeface(AppContext.getInstances().getIconfont());
        iv_navi.setOnClickListener(this);
        iv_navi.setVisibility(View.GONE);

        iv_addpt.setOnClickListener(this);
        iv_addpt.setVisibility(View.GONE);

        iv_move.setOnClickListener(this);
        iv_move.setVisibility(View.GONE);

        iv_addpttoline.setOnClickListener(this);
        iv_addpttoline.setVisibility(View.GONE);

        mZoom.setOnClickListener(this);
        mTipInfo.setOnClickListener(this);
        mLocal.setOnClickListener(this);

        _fromRoute = getIntent().getBooleanExtra(INTENT_ONMAP_FOROMROUTE, _fromRoute);
        _onlySelect = getIntent().getBooleanExtra(INTENT_ONMAP_ONLYSELECT, _onlySelect);
        _fromEdit = getIntent().getBooleanExtra(INTENT_ONMAP_FROMEDIT, _fromEdit);

        _fromAdd = getIntent().getBooleanExtra(INTENT_ONMAP_FROMADD, _fromAdd);

        findViewById(R.id.iv_addpttoline2).setVisibility(View.GONE);
        findViewById(R.id.iv_addpttoline2).setOnClickListener(this);

        if (_fromRoute) {
            showFullRoute();
            iv_navi.setVisibility(View.VISIBLE);
            iv_addpttoline.setVisibility(View.VISIBLE);
        } else if (_fromEdit && DBDataCenter.getInstance().makerEdit != null) {
            _makerEdit = DBDataCenter.getInstance().makerEdit;
            showFromEdit();
            iv_navi.setVisibility(View.VISIBLE);
            iv_move.setVisibility(View.VISIBLE);
        } else {
            String name = getIntent().getStringExtra(INTENT_ONMAP_NAME);
            LonLat ll = (LonLat) getIntent().getSerializableExtra(INTENT_ONMAP_LONLAT);
            if (ll == null) {
                ll = SysUtils.getInstance().getLL();
            }
            if (name == null) {
                name = "";
            }
            if (ll != null && ll.IsValid()) {
                NavFrameSDK.POIDescription data = NavFrameSDK.getInstance().new POIDescription();
                data.name = name;
                data.position.lon = ll.GetLon();
                data.position.lat = ll.GetLat();
                mMultiMapView.setMapCenter(ll);
                mMultiMapView.setScale(100000);
                _OriginalPt = data;
                popTipInfo2(data);
            }
        }

        if (_fromAdd) {
            showFullRoute();
            _fromRoute = true;
            findViewById(R.id.iv_addpttoline2).setVisibility(View.VISIBLE);
            findViewById(R.id.iv_addpttoline2).setFocusable(true);
            mTipInfo.hideBtn();
            mTipInfo.setVisibility(View.GONE);
        }

        if (SysUtils.getInstance().mapsource != 1000 || SysUtils.getInstance().mapsource != 1001) {
            mScale.setVisibility(View.GONE);
        }


        if (_fromEdit) {
            mMultiMapView.mMapViewS.setNeedPopTipInfo(false);//单击无需弹出tip
            mTipInfo.setVisibility(View.GONE);
        }

        if (_fromEdit || _fromRoute || _onlySelect || _fromAdd) {

        } else {
            iv_addpt.setVisibility(View.VISIBLE);
            mMultiMapView.mMapViewS.setNeedPopTipInfo(false);//单击无需弹出tip
            mTipInfo.setVisibility(View.GONE);
        }

    }

    WayPoint _makerEdit = null;//原始的

    private void showFromEdit() {
        LonLat ll = _makerEdit.getLL();
        NavFrameSDK.POIDescription data = NavFrameSDK.getInstance().new POIDescription();
        data.name = _makerEdit.getName();
        data.position.lon = ll.GetLon();
        data.position.lat = ll.GetLat();
        mMultiMapView.setMapCenter(ll);
        mMultiMapView.setScale(100000);
        updateMapView();
//        popTipInfo(data);
        _OriginalPt = data;
    }

    private Route mRouteShow = null;

    boolean _isFirst = true;

    private void showFullRoute() {
        int distance = 0;
        mRouteShow = DBDataCenter.getInstance().routeShow;
        tv_center_lonlat.setText(mRouteShow.getName());
        mLocal.setVisibility(View.GONE);
        mTipInfo.setVisibility(View.GONE);
        if (mRouteShow.waypointList == null) {
            mRouteShow.waypointList = new ArrayList<WayPoint>();
        }
        int size = mRouteShow.waypointList.size();
        if (mRouteShow == null || size < 1) {
            String DIS[] = SysUtils.getInstance().getDistanceStr(distance);
            tv_centertolocal_distance.setText(DIS[0] + DIS[1]);
            return;
        }
        //设置完整航线的地图中心点和比例尺
        if (SysUtils.getInstance().mapsource == 1000) {
            if (_isFirst) {
                DBDataCenter.getInstance().showFullRoute(mRouteShow.waypointList);
            }
            _isFirst = false;
        } else {
            LonLat ll = mRouteShow.waypointList.get(size-1).getLL();
            mMultiMapView.setMapCenter(ll);
        }

        for (int i = 1; i < size; i++) {
            LonLat llA = mRouteShow.waypointList.get(i - 1).getLL();
            LonLat llB = mRouteShow.waypointList.get(i).getLL();
            distance += (int) Start.getInstance().MapKit().getDistance(llA, llB);
        }

        String DIS[] = SysUtils.getInstance().getDistanceStr(distance);
        tv_centertolocal_distance.setText(DIS[0] + DIS[1]);

    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case Msg.SHOWTIP:
                    break;
                case Msg.UPDATEMAP:
                    updateMapView();
                    break;
                case Msg.FINISHUPDATEMAP:
                    finishUpdatedMapView();
                    break;
            }
        }
    };

    private void updateMapView() {
        mMultiMapView.updateMap();
    }

    private void finishUpdatedMapView() {
        if (mMultiMapView.getMapsource() != 1000) {
            mZoom.updateState(0);
        } else {
            mZoom.updateState();
        }
        mScale.setText(mMultiMapView.getScaleString());

        if (_fromRoute)
            return;

        LonLat centerLL = mMultiMapView.getMapCenter();
        if (mMultiMapView.isgcj02) {
            centerLL = Utils.gcj02towgs84(centerLL.GetLon(), centerLL.GetLat());
        }
        LonLat poiLL = new LonLat(_OriginalPt.position.lon, _OriginalPt.position.lat);
        int distance = (int) Start.getInstance().MapKit().getDistance(centerLL, poiLL);
        if (distance < 2) {
            tv_center_lonlat.setText(SysUtils.getInstance().getLLStr(poiLL));

            String DIS[] = SysUtils.getInstance().getDistanceStr(0);
            tv_centertolocal_distance.setText(DIS[0] + DIS[1]);
        } else {
            tv_center_lonlat.setText(SysUtils.getInstance().getLLStr(centerLL));
            String DIS[] = SysUtils.getInstance().getDistanceStr(distance);
            tv_centertolocal_distance.setText(DIS[0] + DIS[1]);
        }
    }

    public void onReturnClick(View v) {
        DBDataCenter.getInstance().makerEdit = _makerEdit;
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnzoomin:
                onZoomIn();
                break;
            case R.id.btnzoomout:
                onZoomOut();
                break;
            case R.id.btnTipcar:
                onTipcar();
                break;
            case R.id.btnTipname:
                onTipname();
                break;
            case R.id.btnTipman:
                onTipman();
                break;
            case R.id.btnlocal:
                onLocal();
                break;
            case R.id.iv_navi:
                onNavi();
                break;
            case R.id.iv_move:
                onMove();
                break;
            case R.id.iv_addpttoline:
                onAddPtToLine();
                break;
            case R.id.iv_addpttoline2:
                onAddPtToLine2();
                break;
            case R.id.iv_addpt:
                onTipname();
                break;
            default:
                break;
        }
    }

    void onAddPtToLine2() {
        if (mRouteShow.waypointList.size() > Route.MAX_WAYPOINT) {
            Toast.makeText(this, getString(R.string.hxzdkybc) + Route.MAX_WAYPOINT + getString(R.string.ghd), Toast.LENGTH_SHORT).show();
            return;
        }

        LonLat centerLL = mMultiMapView.getMapCenter();
        if (mMultiMapView.isgcj02) {
            centerLL = Utils.gcj02towgs84(centerLL.GetLon(), centerLL.GetLat());
        }

        WayPoint wp = DBDataCenter.getInstance().createNewWayPoint(centerLL);
        wp.setId(null);//必须是一个新的航点
        wp.setType(0);
        wp.setRouteid(mRouteShow.getId());
        Long wpId = DBDataCenter.getInstance().saveWayPoint(wp);
        wp.setId(wpId);
        String strWps = mRouteShow.getWaypoint();
        if (strWps.length() < 1) {
            strWps = wp.getId() + "";
        } else {
            strWps += "-" + wp.getId();
        }
        mRouteShow.setWaypoint(strWps);
        mRouteShow.waypointList.add(wp);
        DBDataCenter.getInstance().saveRoute(mRouteShow);

        setResult(Msg.SEACHSELECTEDONE);

        showFullRoute();

        mMultiMapView.setMapCenter(centerLL);
        updateMapView();

//        Toast.makeText(this, R.string.chdyjtj, Toast.LENGTH_SHORT).show();
//        finish();
    }

    void onAddPtToLine() {
        if (mRouteShow.waypointList.size() > Route.MAX_WAYPOINT) {
            Toast.makeText(this, getString(R.string.hxzdkybc) + Route.MAX_WAYPOINT + getString(R.string.ghd), Toast.LENGTH_SHORT).show();
            return;
        }

        LonLat centerLL = mMultiMapView.getMapCenter();
        if (mMultiMapView.isgcj02) {
            centerLL = Utils.gcj02towgs84(centerLL.GetLon(), centerLL.GetLat());
        }

        WayPoint wp = DBDataCenter.getInstance().createNewWayPoint(centerLL);
        wp.setId(null);//必须是一个新的航点
        wp.setType(0);
        wp.setRouteid(mRouteShow.getId());
        Long wpId = DBDataCenter.getInstance().saveWayPoint(wp);
        wp.setId(wpId);
        String strWps = mRouteShow.getWaypoint();
        if (strWps.length() < 1) {
            strWps = wp.getId() + "";
        } else {
            strWps += "-" + wp.getId();
        }
        mRouteShow.setWaypoint(strWps);
        mRouteShow.waypointList.add(wp);
        DBDataCenter.getInstance().saveRoute(mRouteShow);

        showFullRoute();

        mMultiMapView.setMapCenter(centerLL);
        updateMapView();
    }

    void onMove() {
        LonLat centerLL = mMultiMapView.getMapCenter();
        if (mMultiMapView.isgcj02) {
            centerLL = Utils.gcj02towgs84(centerLL.GetLon(), centerLL.GetLat());
        }
        _makerEdit.setLL(centerLL);

        NavFrameSDK.POIDescription data = NavFrameSDK.getInstance().new POIDescription();
        data.name = _makerEdit.getName();
        data.position.lon = centerLL.GetLon();
        data.position.lat = centerLL.GetLat();
        _OriginalPt = data;
        updateMapView();
    }

    void onNavi() {
        if (_fromRoute) {
            if (DBDataCenter.getInstance().routeNavi != null) {
                new AlertDialog(this, "", "确定终止当前航线导航，然后按新航线导航？", true, 12, this).show();
            } else if (DBDataCenter.getInstance().wayTrailNavi != null) {
                new AlertDialog(this, "", "确定终止当前航迹导航，然后按航线导航？", true, 12, this).show();
            } else {
                new AlertDialog(this, "", getString(R.string.to_naviline), true, 12, this).show();
            }
            return;
        }
        WayPoint wt = DBDataCenter.getInstance().makerEdit;
        if (wt == null) {
            return;
        }

        if (DBDataCenter.getInstance().routeNavi != null) {
            new AlertDialog(this, "", getString(R.string.qstopdest) + wt.getName() + "？", true, 11, this).show();
        } else if (DBDataCenter.getInstance().wayTrailNavi != null) {
            new AlertDialog(this, "", getString(R.string.qstopdest_line) + wt.getName() + "？", true, 11, this).show();
        } else {

            new AlertDialog(this, "", getString(R.string.to_navi) + wt.getName() + "？", true, 11, this).show();
//            DBDataCenter.getInstance().makerNavi = wt;
//            setResult(Msg.NAVI_TO_DEST);
//            finish();
        }
    }

    void onZoomIn() {
        mMultiMapView.zoomIn();
        mMultiMapView.updateMap();
    }

    void onZoomOut() {
        mMultiMapView.zoomOut();
        mMultiMapView.updateMap();
    }

    void onLocal() {
        if (Utils.isFastDoubleClick())
            return;

        if (_fromEdit) {
            showFromEdit();
        }
        LonLat poiLL = new LonLat(_OriginalPt.position.lon, _OriginalPt.position.lat);
        mMultiMapView.setScale(100000);
        mMultiMapView.setMapCenter(poiLL);
        mMultiMapView.updateMap();
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            default:
                break;
            case 23://ok
                if (_fromAdd) {
                    onAddPtToLine2();
                }
                break;
            case KeyEvent.KEYCODE_BACK:
                finish();
                break;
            case 24://Zoomin
                if (!SysUtils.getInstance().is240x320) {
                    return true;
                }
                onZoomIn();
                return true;
            case 25://Zoomout
                if (!SysUtils.getInstance().is240x320) {
                    return true;
                }
                onZoomOut();
                return true;
        }

        return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            default:
                break;
            case 21://left
                mMultiMapView.moveMap(-5, 0);
                mTipInfo.setVisibility(View.GONE);
                return true;
            case 22://right
                mMultiMapView.moveMap(5, 0);
                mTipInfo.setVisibility(View.GONE);
                return true;
            case 19://up
                mMultiMapView.moveMap(0, -5);
                mTipInfo.setVisibility(View.GONE);
                return true;
            case 20://down
                mMultiMapView.moveMap(0, 5);
                mTipInfo.setVisibility(View.GONE);
                return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    void onTipcar() {
        if (Utils.isFastDoubleClick())
            return;
        NavFrameSDK.POIDescription data = mTipInfo.getData();
        if (data == null) {
            return;
        }

    }

    void onTipname() {
        if (Utils.isFastDoubleClick())
            return;
        NavFrameSDK.POIDescription data = mTipInfo.getData();
        if (data == null) {
            return;
        }

        if (_onlySelect) {
//            setResult();
//            finish(); //目前还没有这样的需求，暂不做，只是不需要继续添加航点
        } else {
            if (_makerEdit != null) {
                LonLat ll = _makerEdit.getLL();
                if (ll.IsSame(data.position.lon, data.position.lat)) {
                    startActivityForResult(MarkerPointActivity.createIntentNoMenu(this), 0);
                    return;
                }
            }

            DBDataCenter.getInstance().makerEdit = new WayPoint(data);
            startActivityForResult(MarkerPointActivity.createIntentNoMenu(this), 0);
        }
    }

    void onTipman() {
        if (Utils.isFastDoubleClick())
            return;
        NavFrameSDK.POIDescription data = mTipInfo.getData();
        if (data == null) {
            return;
        }
    }

    @Override
    public void popTipInfo(NavFrameSDK.POIDescription data) {
        if (_fromRoute || _fromAdd) {
            return;
        }
        mTipInfo.hideBtn();//是否显示两边的按钮
        mTipInfo.setData(data);

    }

    void popTipInfo2(NavFrameSDK.POIDescription data) {
        if (data == null || _fromAdd) {
            return;
        }

        NavFrameSDK.POIDescription data2 = NavFrameSDK.getInstance().new POIDescription();
        data2.name = data.name;
        LonLat ll = new LonLat(data.position.lon, data.position.lat);
        ll = Utils.wgs84togcj02(ll.GetLon(), ll.GetLat());//Utils GPS to 火星坐标
        data2.position.lon = ll.GetLon();
        data2.position.lat = ll.GetLat();

        mTipInfo.hideBtn();//是否显示两边的按钮
        mTipInfo.setData(data2);
    }

    @Override
    public void moveMap(boolean removeTip) {
        if (removeTip) {
            mTipInfo.setVisibility(View.GONE);
        }
    }

    @Override
    public void touchUp() {

    }

    @Override
    public void longPress(LonLat ll) {
        mTipInfo.setVisibility(View.GONE);
    }

    @Override
    public void doubleClick(LonLat ll) {
    }

    @Override
    public void singleClick(LonLat ll, Point tipPoint) {

    }

    Compass mCompass = new Compass();

    @Override
    public void drawOverlay(Canvas canvas, float ws) {

        if (mMultiMapView.getMapsource() != 1000) {
            return;
        }

        MapSetInfo.getInstance().draw(this, canvas, ws, mMultiMapView.mMapViewS);

        mCompass.draw(this, canvas, ws, ws);

        if (!_fromRoute) {
            drawLineOri2Center(canvas, ws, mMultiMapView.mMapViewS);
            drawOriginalPt(canvas, ws);
        }

        if (_fromRoute) {
            drawRoute(canvas, ws);//画航线
        } else {
            drawMarker(canvas, ws);
        }
        //画完图
        mHandler.sendEmptyMessage(Msg.FINISHUPDATEMAP);
    }

    private void drawRoute(Canvas canvas, float ws) {
        PathEffect pathEffect = new DashPathEffect(new float[]{5, 5, 5, 5}, 1);
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(8);
        paint.setAntiAlias(true);
        paint.setPathEffect(pathEffect);

        int count = DBDataCenter.getInstance().routeShow.waypointList.size();
        Point ffA = null;
        for (int i = 0; i < count; ++i) {
            WayPoint wp = DBDataCenter.getInstance().routeShow.waypointList.get(i);
            LonLat poiLL = wp.getLL();
            poiLL = Utils.wgs84togcj02(poiLL.GetLon(), poiLL.GetLat());//Utils GPS to 火星坐标
            Point ffB = Start.getInstance().MapKit().LL2XY(poiLL);

            //画线段,一段段判断+画
            if (i > 0) {
                paint.setStyle(Paint.Style.STROKE);
                paint.setColor(Color.parseColor("#AA0000FF"));
                canvas.drawLine(ffA.x * ws, ffA.y * ws, ffB.x * ws, ffB.y * ws, paint);
            }

            ffA = ffB;
        }

        for (int i = 0; i < count; ++i) {
            WayPoint wp = DBDataCenter.getInstance().routeShow.waypointList.get(i);
            LonLat poiLL = wp.getLL();
            poiLL = Utils.wgs84togcj02(poiLL.GetLon(), poiLL.GetLat());//Utils GPS to 火星坐标
            Point ffB = Start.getInstance().MapKit().LL2XY(poiLL);

            //画icon
            if (mMultiMapView.mMapViewS.isInMapRegion(poiLL)) {
                paint.setStyle(Paint.Style.FILL);
                paint.setColor(Color.parseColor("#AAFF0000"));

                paint.setTextSize(16);
                int w1 = 16;
                if (SysUtils.getInstance().is240x320) {
                    paint.setTextSize(11);
                    w1 = 8;
                }

                String strPrn = wp.getName();
                float textWidth = paint.measureText(strPrn);
                float textHeight = paint.getTextSize() / 2;

                if (SysUtils.getInstance().is240x320) {
                    if (textWidth < 10) {
                        textWidth = 10;
                    }
                }

                canvas.drawRect(ffB.x * ws - textWidth / 2,
                        ffB.y * ws - w1,
                        ffB.x * ws + textWidth / 2,
                        ffB.y * ws + w1,
                        paint);

                paint.setColor(Color.WHITE);
                paint.setFakeBoldText(true);
                canvas.drawText(strPrn, ffB.x * ws - textWidth / 2, ffB.y * ws  + 4, paint);
            }
        }
    }

    private void drawMarker(Canvas canvas, float ws) {
        if (_makerEdit == null) {
            return;
        }

        WayPoint wp = _makerEdit;
        LonLat poiLL = wp.getLL();
        poiLL = Utils.wgs84togcj02(poiLL.GetLon(), poiLL.GetLat());//Utils GPS to 火星坐标
        if (!mMultiMapView.mMapViewS.isInMapRegion(poiLL)) {
            return;
        }
        int resId = wp.getTypeIcon();
        Bitmap bm = ((BitmapDrawable) getResources().getDrawable(resId))
                .getBitmap();
        Point e = Start.getInstance().MapKit().LL2XY(poiLL);
        int x = (int) (e.x * ws - bm.getWidth() / 2);
        int y = (int) (e.y * ws - bm.getHeight() / 2);
        canvas.drawBitmap(bm, x, y, null);

        Paint paint = new Paint();
        paint.reset();
        int lp = 32;
        paint.setTextSize(13);
        if (SysUtils.getInstance().is240x320) {
            paint.setTextSize(11);
            lp = 16;
        }
        paint.setColor(Color.BLUE);
        paint.setFakeBoldText(true);
        paint.setAntiAlias(true);
        paint.setAlpha(220);
        paint.setUnderlineText(true);
//              paint.measureText(wp.getName());
        String name = wp.getName();
        if (name.length() > 13) {
            name = wp.getName().substring(0, 13);
        }
        canvas.drawText(name, x + bm.getWidth() - 2, y + lp, paint);
    }

    public void drawLineOri2Center(Canvas canvas, float ws, MapViewS mapView) {
        if (_OriginalPt == null)
            return;

        int w = mapView._mapWidth;
        int h = mapView._mapHeight;

        LonLat poiLLA = new LonLat(_OriginalPt.position.lon, _OriginalPt.position.lat);

        poiLLA = Utils.wgs84togcj02(poiLLA.GetLon(), poiLLA.GetLat());//Utils GPS to 火星坐标

        LonLat poiLLB = Start.getInstance().MapKit().getMapCenter();

        double[] lineLL = {poiLLA.GetLon(), poiLLA.GetLat(), poiLLB.GetLon(), poiLLB.GetLat()};
        if (!Utils.isLineIntersectRectangle(mapView.getMapRegion(), lineLL)) {
            return;
        }

        PathEffect pathEffect = new DashPathEffect(new float[]{20, 10, 5, 10}, 0);
        Paint paint = new Paint();
        paint.reset();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(4);
        paint.setColor(Color.GRAY);
        paint.setAntiAlias(true);
        paint.setPathEffect(pathEffect);
        Path path = new Path();

        Point A = Start.getInstance().MapKit().LL2XY(poiLLA);//耗时操作能省则省
        Point B = Start.getInstance().MapKit().LL2XY(poiLLB);
        int[] line = {A.x, A.y, B.x, B.y};
        if (Utils.revisePt(line, w, h)) {
            path.moveTo(line[0] * ws, line[1] * ws);
            path.lineTo(line[2] * ws, line[3] * ws);
        }
        canvas.drawPath(path, paint);
    }

    //显示原定位点
    void drawOriginalPt(Canvas canvas, float ws) {
        if (_OriginalPt == null)
            return;

        LonLat poiLL = new LonLat(_OriginalPt.position.lon, _OriginalPt.position.lat);

        poiLL = Utils.wgs84togcj02(poiLL.GetLon(), poiLL.GetLat());//Utils GPS to 火星坐标

        if (!mMultiMapView.mMapViewS.isInMapRegion(poiLL)) {
            return;
        }

        Bitmap bm = ((BitmapDrawable) getResources().getDrawable(R.drawable.map_marker2))
                .getBitmap();
        Point e = Start.getInstance().MapKit().LL2XY(poiLL);
        int x = (int) (e.x * ws - bm.getWidth() / 2);
        int y = (int) (e.y * ws - bm.getHeight());
        canvas.drawBitmap(bm, x, y, null);
    }

    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {
        if (isPositive == false) {
            return;
        }

        if (requestCode == 11) {
            WayPoint wt = DBDataCenter.getInstance().makerEdit;
            if (wt == null) {
                return;
            }

            DBDataCenter.getInstance().routeNavi = null;
            DBDataCenter.getInstance().wayTrailNavi = null;
            Start.getInstance().setHavePath(false);
            DBDataCenter.getInstance().makerNavi = wt;
            setResult(Msg.NAVI_TO_DEST);
            finish();
        } else if (requestCode == 12) {
            DBDataCenter.getInstance().makerNavi = null;
            DBDataCenter.getInstance().wayTrailNavi= null;
            Start.getInstance().setHavePath(false);
            DBDataCenter.getInstance().routeNavi = DBDataCenter.getInstance().routeShow;
            DBDataCenter.getInstance().routeNaviIndex = 0;
            SysUtils.getInstance().startRouteNavi();
            setResult(Msg.NAVI_BY_ROUTE);
            finish();
        }
    }

    @Override
    public void onDestroy() {
        mMultiMapView.destroyMap();
        super.onDestroy();
    }

    //====================
    private void initMapEngin() {
        mGisOverlay = new CustomPointOverlay();
        mMultiMapView.mMapViewOnline.getOverlays().add(mGisOverlay);
        mMultiMapView.mapView.addLayer(mapsforgeLayer);

        mMultiMapView.setMapCallback(this, mapListener);
    }

    //======OSM
    MapListener mapListener = new MapListener() {
        @Override
        public boolean onScroll(ScrollEvent scrollEvent) {

            return true;
        }

        @Override
        public boolean onZoom(ZoomEvent zoomEvent) {
            return false;
        }

        @Override
        public void longPress(Point pt) {
            if (pt == null) {
                return;
            }
        }

        //双击
        @Override
        public void doubleClick(Point pt) {
            if (pt == null) {
                return;
            }
        }

        //单击 单击后
        @Override
        public void singleClick(Point pt) {
            if (pt == null) {
                return;
            }

            _tipPoint = pt;
            resetTipInfo2(true);
        }

        //移图后抬起
        @Override
        public void touchUp() {
        }

        //手指移动-切换到移图模式
        @Override
        public void moveMap() {
        }

    };

    Point _tipPoint = null;

    void resetTipInfo2(boolean searching) {
        if (_tipPoint == null || _fromEdit || _fromAdd || _fromRoute) {
            return;
        }

        LonLat tipLL = null;//原始；
        LonLat tipLLH = null;//火星；

        if (SysUtils.getInstance().mapsource == 1001) {
            LatLong geoPoint = mMultiMapView.mapView.getMapViewProjection().fromPixels(_tipPoint.x, _tipPoint.y);
            tipLL = new LonLat(geoPoint.getLongitude(), geoPoint.getLatitude());
        } else {
            IGeoPoint geoPoint = mMultiMapView.mMapViewOnline.getProjection().fromPixels(_tipPoint.x, _tipPoint.y);
            tipLL = new LonLat(geoPoint.getLongitude(), geoPoint.getLatitude());
        }

        if (mMultiMapView.isgcj02) {
            tipLLH = tipLL;
            tipLL = Utils.gcj02towgs84(tipLL.GetLon(), tipLL.GetLat());
        } else {
            tipLLH = Utils.wgs84togcj02(tipLL.GetLon(), tipLL.GetLat());
        }

        if (searching) {
            //5米以内的poi
            Start.getInstance().MapKit().mTipPoiList = Start.getInstance().MapKit()
                    .getNearPoi(tipLLH, true);
            if (Start.getInstance().MapKit().mTipPoiList.size() > 0) {
                NavFrameSDK.POIDescription data = Start.getInstance().MapKit().mTipPoiList.get(0);
//                mTipInfo.showBtn();//是否显示两边的按钮
                mTipInfo.setData(data);
            }
        }

        mTipInfo.setLL(tipLL);
        if (mHandler != null) {
            mHandler.sendEmptyMessage(Msg.CHANGEMAPINFO);
        }

        mMultiMapView.mMapViewOnline.invalidate();
        mMultiMapView.mapView.invalidate();
    }

    private CustomPointOverlay mGisOverlay = null;

    public class CustomPointOverlay extends Overlay {

        private final Point mMapCoordsProjected = new Point();

        public CustomPointOverlay() {
        }

        @Override
        public void draw(Canvas canvas, MapView mapView, boolean shadow) {

            if (mMultiMapView.getMapsource() >= 1000 && mMultiMapView.getMapsource() <= 1001) {
                return;
            }

            MapSetInfo.getInstance().draw2(DMapActivity.this, canvas, mapView);

            mCompass.draw2(DMapActivity.this, canvas, (int) mapView.getMapOrientation());

            if (!_fromRoute) {
                drawLineOri2Center2(canvas);
                drawOriginalPt2(canvas);
            }

            if (_fromRoute) {
                drawRoute2(canvas);//画航线
            } else {
                drawMarker2(canvas);
            }

            //画完图
            mHandler.sendEmptyMessage(Msg.FINISHUPDATEMAP);
        }

        //经纬度坐标到屏幕坐标的转换
        Point LL2XY(LonLat ll) {
            Point B = new Point();
            mMultiMapView.mMapViewOnline.getProjection().toProjectedPixels(ll.GetLat(),
                    ll.GetLon(), mMapCoordsProjected);
            Projection pj = mMultiMapView.mMapViewOnline.getProjection();
            pj.toPixelsFromProjected(mMapCoordsProjected, B);
            return B;
        }

        void drawRoute2(Canvas canvas) {
            if (DBDataCenter.getInstance().routeShow == null || DBDataCenter.getInstance().routeShow.waypointList == null) {
                return; //为什么一直在画？？
            }
            
            PathEffect pathEffect = new DashPathEffect(new float[]{5, 5, 5, 5}, 1);
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
            if (SysUtils.getInstance().is240x320) {
                paint.setStrokeWidth(3);
            } else {
                paint.setStrokeWidth(8);
            }
            paint.setAntiAlias(true);
            paint.setPathEffect(pathEffect);

            int count = DBDataCenter.getInstance().routeShow.waypointList.size();
            int resId;
            Point ffA = null;
            for (int i = 0; i < count; ++i) {
                WayPoint wp = DBDataCenter.getInstance().routeShow.waypointList.get(i);

                LonLat poiLL = wp.getLL();
                if (mMultiMapView.isgcj02) {
                    poiLL = Utils.wgs84togcj02(poiLL.GetLon(), poiLL.GetLat());//Utils GPS to 火星坐标
                }
                Point ffB = LL2XY(poiLL);

                //画线段,一段段判断+画
                if (i > 0) {
                    paint.setStyle(Paint.Style.STROKE);
                    paint.setColor(Color.parseColor("#AA0000FF"));
                    canvas.drawLine(ffA.x, ffA.y, ffB.x, ffB.y, paint);
                }

                ffA = ffB;
            }

            for (int i = 0; i < count; ++i) {
                WayPoint wp = DBDataCenter.getInstance().routeShow.waypointList.get(i);
                LonLat poiLL = wp.getLL();
                if (mMultiMapView.isgcj02) {
                    poiLL = Utils.wgs84togcj02(poiLL.GetLon(), poiLL.GetLat());//Utils GPS to 火星坐标
                }
                Point ffB = LL2XY(poiLL);

                //画icon
                if (ffB.x > 0 && ffB.x < canvas.getWidth() && ffB.y > 0 && ffB.y < canvas.getHeight()) {
                    paint.setStyle(Paint.Style.FILL);
                    paint.setColor(Color.parseColor("#AAFF0000"));

                    int w1 = 16;
                    if (SysUtils.getInstance().is240x320) {
                        paint.setTextSize(10);
                        w1 = 8;
                    } else {
                        paint.setTextSize(16);
                    }

                    String strPrn = wp.getName();
                    float textWidth = paint.measureText(strPrn);
                    float textHeight = paint.getTextSize() / 2;
                    canvas.drawRect(ffB.x - textWidth / 2,
                            ffB.y - w1,
                            ffB.x + textWidth / 2,
                            ffB.y + w1,
                            paint);

                    paint.setColor(Color.WHITE);
                    paint.setFakeBoldText(true);
                    canvas.drawText(strPrn, ffB.x - textWidth / 2, ffB.y + 4, paint);
                }
            }

        }

        void drawMarker2(Canvas canvas) {
            if (_makerEdit == null) {
                return;
            }
            LonLat poiLL = _makerEdit.getLL();
            Point e = LL2XY(poiLL);
            if (e.x < 0|| e.x > canvas.getWidth() || e.y < 0 && e.y > canvas.getHeight()) {
                return;
            }
            int resId = _makerEdit.getTypeIcon();
            Bitmap bm = ((BitmapDrawable) getResources().getDrawable(resId))
                    .getBitmap();

            int x = (int) (e.x - bm.getWidth() / 2);
            int y = (int) (e.y - bm.getHeight() / 2);
            canvas.drawBitmap(bm, x, y, null);

            Paint paint = new Paint();
            paint.reset();
            int lp = 32;
            paint.setTextSize(13);
            if (SysUtils.getInstance().is240x320) {
                paint.setTextSize(11);
                lp = 16;
            }
            paint.setColor(Color.BLUE);
            paint.setFakeBoldText(true);
            paint.setAntiAlias(true);
            paint.setAlpha(220);
            paint.setUnderlineText(true);
//              paint.measureText(wp.getName());
            String name = _makerEdit.getName();
            if (name.length() > 13) {
                name = _makerEdit.getName().substring(0, 13);
            }
            canvas.drawText(name, x + bm.getWidth() - 2, y + lp, paint);

        }

        void drawLineOri2Center2(Canvas canvas) {
            if (_OriginalPt == null)
                return;

            LonLat poiLLA = new LonLat(_OriginalPt.position.lon, _OriginalPt.position.lat);
            if (mMultiMapView.isgcj02) {
                poiLLA = Utils.wgs84togcj02(poiLLA.GetLon(), poiLLA.GetLat());//Utils GPS to 火星坐标
            }
            LonLat poiLLB = mMultiMapView.getMapCenter();

            BoundingBox bb = mMultiMapView.mMapViewOnline.getBoundingBox();//获取当前区域
            double[] regions = {bb.getLonEast(), bb.getLatNorth(), bb.getLonWest(), bb.getLatSouth()};

            double[] lineLL = {poiLLA.GetLon(), poiLLA.GetLat(), poiLLB.GetLon(), poiLLB.GetLat()};
            if (!Utils.isLineIntersectRectangle(regions, lineLL)) {
                return;
            }

            PathEffect pathEffect = new DashPathEffect(new float[]{20, 10, 5, 10}, 0);
            Paint paint = new Paint();
            paint.reset();
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(4);
            paint.setColor(Color.GRAY);
            paint.setAntiAlias(true);
            paint.setPathEffect(pathEffect);
            Path path = new Path();

            Point A = LL2XY(poiLLA);//耗时操作能省则省
            Point B = LL2XY(poiLLB);
            int[] line = {A.x, A.y, B.x, B.y};
            if (Utils.revisePt(line, canvas.getWidth(), canvas.getHeight())) {
                path.moveTo(line[0], line[1]);
                path.lineTo(line[2], line[3]);
            }
            canvas.drawPath(path, paint);
        }

        //显示原定位点
        void drawOriginalPt2(Canvas canvas) {
            if (_OriginalPt == null)
                return;

            LonLat poiLL = new LonLat(_OriginalPt.position.lon, _OriginalPt.position.lat);
            if (mMultiMapView.isgcj02) {
                poiLL = Utils.wgs84togcj02(poiLL.GetLon(), poiLL.GetLat());//Utils GPS to 火星坐标
            }
            Point e = LL2XY(poiLL);
            Bitmap bm = ((BitmapDrawable) getResources().getDrawable(R.drawable.map_marker2))
                    .getBitmap();
            int x = (int) (e.x - bm.getWidth() / 2);
            int y = (int) (e.y - bm.getHeight());

            if (x > 0 && x < canvas.getWidth() && y > 0 && y < canvas.getHeight()) {
                canvas.drawBitmap(bm, x, y, null);
            }
        }

    }

    //======Mapsforge
    MapsforgeLayer mapsforgeLayer = new MapsforgeLayer();

    public class MapsforgeLayer extends Layer {

        org.mapsforge.core.model.BoundingBox bb;
        byte zoomLevel;
        org.mapsforge.core.graphics.Canvas canvas;
        org.mapsforge.core.model.Point topLeftPoint;

        public MapsforgeLayer() {
        }

        @Override
        public synchronized void draw(org.mapsforge.core.model.BoundingBox bb, byte zoomLevel, org.mapsforge.core.graphics.Canvas canvas, org.mapsforge.core.model.Point topLeftPoint) {

            if (mMultiMapView.getMapsource() != 1001) {
                return;
            }

            this.bb = bb;
            this.zoomLevel = zoomLevel;
            this.canvas = canvas;
            this.topLeftPoint = topLeftPoint;

            MapSetInfo.getInstance().draw3(DMapActivity.this, canvas, bb, zoomLevel, topLeftPoint, displayModel);
            mCompass.draw3(DMapActivity.this, canvas, 0, bb, zoomLevel, topLeftPoint, displayModel);

            android.graphics.Canvas androidCanvas = AndroidGraphicFactory.getCanvas(canvas);
            if (!_fromRoute) {
                drawLineOri2Center3(androidCanvas);
                drawOriginalPt3(androidCanvas);
            }
            
            if (_fromRoute) {
                drawRoute3(androidCanvas);//画航线
            } else {
                drawMarker3(androidCanvas);
            }

            //画完图
            mHandler.sendEmptyMessage(Msg.FINISHUPDATEMAP);
        }

        //经纬度坐标到屏幕坐标的转换
        Point LL2XY(LonLat ll) {
            long mapSize = MercatorProjection.getMapSize(zoomLevel, displayModel.getTileSize());
            int x = (int) (MercatorProjection.longitudeToPixelX(ll.GetLon(), mapSize) - topLeftPoint.x);
            int y = (int) (MercatorProjection.latitudeToPixelY(ll.GetLat(), mapSize) - topLeftPoint.y);
            Point B = new Point(x, y);
            return B;
        }

        void drawRoute3(Canvas canvas) {
            if (DBDataCenter.getInstance().routeShow == null || DBDataCenter.getInstance().routeShow.waypointList == null) {
                return; //为什么一直在画？？
            }
            PathEffect pathEffect = new DashPathEffect(new float[]{5, 5, 5, 5}, 1);
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
            if (SysUtils.getInstance().is240x320) {
                paint.setStrokeWidth(3);
            } else {
                paint.setStrokeWidth(8);
            }
            paint.setAntiAlias(true);
            paint.setPathEffect(pathEffect);

            int count = DBDataCenter.getInstance().routeShow.waypointList.size();
            int resId;
            Point ffA = null;
            for (int i = 0; i < count; ++i) {
                WayPoint wp = DBDataCenter.getInstance().routeShow.waypointList.get(i);
                LonLat poiLL = wp.getLL();
                Point ffB = LL2XY(poiLL);

                //画线段,一段段判断+画
                if (i > 0) {
                    paint.setStyle(Paint.Style.STROKE);
                    paint.setColor(Color.parseColor("#AA0000FF"));
                    canvas.drawLine(ffA.x, ffA.y, ffB.x, ffB.y, paint);
                }

                ffA = ffB;
            }

            for (int i = 0; i < count; ++i) {
                WayPoint wp = DBDataCenter.getInstance().routeShow.waypointList.get(i);
                LonLat poiLL = wp.getLL();
                Point ffB = LL2XY(poiLL);

                //画icon
                if (ffB.x > 0 && ffB.x < canvas.getWidth() && ffB.y > 0 && ffB.y < canvas.getHeight()) {
                    paint.setStyle(Paint.Style.FILL);
                    paint.setColor(Color.parseColor("#AAFF0000"));

                    int w1 = 16;
                    if (SysUtils.getInstance().is240x320) {
                        paint.setTextSize(10);
                        w1 = 8;
                    } else {
                        paint.setTextSize(16);
                    }
//                String strPrn = (i + 1) + "";
                    String strPrn = wp.getName();
                    float textWidth = paint.measureText(strPrn);
                    float textHeight = paint.getTextSize() / 2;

//              canvas.drawCircle(ffB.x * ws, ffB.y * ws, 16, paint);
                    canvas.drawRect(ffB.x - textWidth / 2,
                            ffB.y - w1,
                            ffB.x + textWidth / 2,
                            ffB.y + w1,
                            paint);

                    paint.setColor(Color.WHITE);
                    paint.setFakeBoldText(true);
                    canvas.drawText(strPrn, ffB.x - textWidth / 2, ffB.y + 4, paint);
                }
            }

        }

        void drawMarker3(Canvas canvas) {

            if (_makerEdit == null) {
                return;
            }
            LonLat poiLL = _makerEdit.getLL();
            Point e = LL2XY(poiLL);
            if (e.x < 0|| e.x > canvas.getWidth() || e.y < 0 && e.y > canvas.getHeight()) {
                return;
            }
            int resId = _makerEdit.getTypeIcon();
            Bitmap bm = ((BitmapDrawable) getResources().getDrawable(resId))
                    .getBitmap();

            int x = (int) (e.x - bm.getWidth() / 2);
            int y = (int) (e.y - bm.getHeight() / 2);
            canvas.drawBitmap(bm, x, y, null);

            Paint paint = new Paint();
            paint.reset();
            int lp = 32;
            paint.setTextSize(13);
            if (SysUtils.getInstance().is240x320) {
                paint.setTextSize(11);
                lp = 16;
            }
            paint.setColor(Color.BLUE);
            paint.setFakeBoldText(true);
            paint.setAntiAlias(true);
            paint.setAlpha(220);
            paint.setUnderlineText(true);
//              paint.measureText(wp.getName());
            String name = _makerEdit.getName();
            if (name.length() > 13) {
                name = _makerEdit.getName().substring(0, 13);
            }
            canvas.drawText(name, x + bm.getWidth() - 2, y + lp, paint);
        }

        void drawLineOri2Center3(Canvas canvas) {
            if (_OriginalPt == null)
                return;

            LonLat poiLLA = new LonLat(_OriginalPt.position.lon, _OriginalPt.position.lat);
            LonLat poiLLB = mMultiMapView.getMapCenter();

            double[] regions = {bb.maxLongitude, bb.maxLatitude, bb.minLongitude, bb.minLatitude};

            double[] lineLL = {poiLLA.GetLon(), poiLLA.GetLat(), poiLLB.GetLon(), poiLLB.GetLat()};
            if (!Utils.isLineIntersectRectangle(regions, lineLL)) {
                return;
            }

            PathEffect pathEffect = new DashPathEffect(new float[]{20, 10, 5, 10}, 0);
            Paint paint = new Paint();
            paint.reset();
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(4);
            paint.setColor(Color.GRAY);
            paint.setAntiAlias(true);
            paint.setPathEffect(pathEffect);
            Path path = new Path();

            Point A = LL2XY(poiLLA);//耗时操作能省则省
            Point B = LL2XY(poiLLB);
            int[] line = {A.x, A.y, B.x, B.y};
            if (Utils.revisePt(line, canvas.getWidth(), canvas.getHeight())) {
                path.moveTo(line[0], line[1]);
                path.lineTo(line[2], line[3]);
            }
            canvas.drawPath(path, paint);
        }

        //显示原定位点
        void drawOriginalPt3(Canvas canvas) {
            if (_OriginalPt == null)
                return;

            LonLat poiLL = new LonLat(_OriginalPt.position.lon, _OriginalPt.position.lat);

            Point e = LL2XY(poiLL);
            Bitmap bm = ((BitmapDrawable) getResources().getDrawable(R.drawable.map_marker2))
                    .getBitmap();
            int x = (int) (e.x - bm.getWidth() / 2);
            int y = (int) (e.y - bm.getHeight());

            if (x > 0 && x < canvas.getWidth() && y > 0 && y < canvas.getHeight()) {
                canvas.drawBitmap(bm, x, y, null);
            }
        }

    }

}
